import Vue from 'vue'
import Router from 'vue-router'
import { getSyncToken } from '@/api/util'
import { isLogin } from '@/util/index'
import store from '../store'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    { 
      path: '/',
      name: 'index',
      component: resolve => require(['@/pages/index/index'], resolve),
      meta: {
        title: '',
        auth: false
      }
    },
    { 
      path: '/projectPrev',
      name: 'projectPrev',
      component: resolve => require(['@/pages/index/projectPrev'], resolve),
      meta: {
        title: '',
        auth: false
      }
    },
  ],
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.meta.auth) {
    let token = getSyncToken()
    if (token && isLogin()) {
      next()
    } else {
      document.title = '正在登陆...'
      store.commit('SET_LOGINGO', to.fullPath)
      // router.push({
      //   path: '/login'
      // })
    }
  } else {
    next()
  }
})

export default router
